#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define endl "\n"
namespace DBG
{
	template <class T>
	ostream& operator << (ostream& os,const vector<T> &v)
	{
		os<<"[ ";
		for (int i=0;i<v.size();i++)
		{
			os<<v[i];
			if (i+1<v.size())
				os<<", ";
		}
		os<<" ]";
		return os;
	}

	template <class T,size_t N>
	ostream& operator << (ostream& os,const array<T,N> &v)
	{
		os<<"[ ";
		for (int i=0;i<v.size();i++)
		{
			os<<v[i];
			if (i+1<v.size())
				os<<", ";
		}
		os<<" ]";
		return os;
	}

	template <class T>
	void _dbg(const char *f,T t)
	{
		cerr<<f<<'='<<t<<'\n';
	}

	template <class A,class... B>
	void _dbg(const char *f,A a,B... b)
	{
		while (*f!=',') cerr<<*f++;
		cerr<<'='<<a<<",";
		_dbg(f+1,b...);
	}

	#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
}

using namespace DBG;
void solve(){
    ll l,r;
    cin>>l>>r;
    vector<ll>a(r-l+5,-1);
    for(ll i=r;i>=l;i--){
    	if(a[i-l]!=-1)continue;
    	ll temp=i;
    	int f=0;
    	ll ans=r-i+l;
    	ll t=-1;
    	for(ll j=32;j>=0;j--){
    		if(temp&(1LL<<j))f=1;
    		if(f){
				if((temp^(1LL<<j))>=l&&(temp^(1LL<<j))<=r)temp^=(1LL<<j);
    		}
    		if(f)dbg(temp,i^temp);
    		if(f&&a[temp-l]==-1&&(i^temp)>t){
    			ans=temp;
    			t=i^temp;
    		}
    	}
    	dbg(i-l,ans);
    	a[i-l]=ans;
    	a[ans-l]=i;
    }
    ll sum=0;
    for(ll i=0;i<=r-l;i++){
    	// dbg(a[i],i+l,a[i]|(i+l));
    	sum+=a[i]|(i+l);
    }
    cout<<sum<<endl;
    for(int i=0;i<=r-l;i++){
    	cout<<a[i]<<" ";
    }
    cout<<endl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}